﻿using DevExpress.Utils;
using MDIDemo.Model;
using MDIDemo.PublicClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_strongPara : Form_BaseAlone
    {
        public Form_strongPara(string FormText)
        {
            InitializeComponent();
            SetComponent(FormText);
        }
        private WaitDialogForm waitDialogForm;
        private Class_MySqlDataBase class_MySqlDataBase;
        private void SetComponent(string FormText)
        {
            base.SetComponent(FormText, "I1006");
            base.SetGridDataSource(null);
            base.GetGridPage().PageSize = 50;
            base.EnableViewButton(false);
            base.EnableOutButton(false);

            Class_PublicMethod class_PublicMethod = new Class_PublicMethod();
            Class_DataBaseConDefault class_DataBaseConDefault = new Class_DataBaseConDefault();
            class_DataBaseConDefault = class_PublicMethod.FromXmlToDefaultValueObject<Class_DataBaseConDefault>("DataBaseDefaultValues");

            class_MySqlDataBase = new Class_MySqlDataBase(class_DataBaseConDefault.dataSourceUrl
                , class_DataBaseConDefault.dataBaseName
                , class_DataBaseConDefault.dataSourceUserName
                , class_DataBaseConDefault.dataSourcePassWord
                , class_DataBaseConDefault.Port);
        }
        public override int AddData()
        {
            int index = -1;
            Form_stongParaBase form_StongParaBase = new Form_stongParaBase();
            form_StongParaBase.SetWinType(1);
            form_StongParaBase.id = null;
            form_StongParaBase.Ini();
            form_StongParaBase.Text = this.Text;
            if (form_StongParaBase.ShowDialog() == DialogResult.OK)
            {
                string Sql = string.Format(@"insert into strongpara
                (englishAll
                ,id
                ,englishName
                ,chinaName
                ,paraType
                ,smallPoint
                ,site
                ,sortNo
                ,strongName
                ,strongClass
                ) values
                ('{0}','{1}', '{2}','{3}','{4}', {5},'{6}',{7},'{8}','{9}') "
                    , form_StongParaBase.strongParaModel.englishAll
                    , form_StongParaBase.strongParaModel.id
                    , form_StongParaBase.strongParaModel.englishName
                    , form_StongParaBase.strongParaModel.chinaName
                    , form_StongParaBase.strongParaModel.paraType
                    , form_StongParaBase.strongParaModel.smallPoint
                    , form_StongParaBase.strongParaModel.site
                    , form_StongParaBase.strongParaModel.sortNo
                    , form_StongParaBase.strongParaModel.strongName
                    , form_StongParaBase.strongParaModel.strongClass
                    );
                index = class_MySqlDataBase.ExecuteSql(Sql);
            }
            form_StongParaBase.Dispose();
            return index;
        }
        public override int EditData(object id)
        {
            int index = -1;
            Form_stongParaBase form_StongParaBase = new Form_stongParaBase();
            form_StongParaBase.SetWinType(0);
            form_StongParaBase.id = id.ToString();
            form_StongParaBase.Ini();
            form_StongParaBase.Text = this.Text;
            if (form_StongParaBase.ShowDialog() == DialogResult.OK)
            {
                string Sql = string.Format(@"Update strongpara
                    SET englishAll='{1}',
                    englishName='{2}',
                    chinaName='{3}',
                    paraType='{4}',
                    smallPoint={5},
                    site='{6}',
                    sortNo={7},
                    strongName='{8}',
                    strongClass='{9}'
                    WHERE id='{0}'"
                    , form_StongParaBase.strongParaModel.id
                    , form_StongParaBase.strongParaModel.englishAll
                    , form_StongParaBase.strongParaModel.englishName
                    , form_StongParaBase.strongParaModel.chinaName
                    , form_StongParaBase.strongParaModel.paraType
                    , form_StongParaBase.strongParaModel.smallPoint
                    , form_StongParaBase.strongParaModel.site
                    , form_StongParaBase.strongParaModel.sortNo
                    , form_StongParaBase.strongParaModel.strongName
                    , form_StongParaBase.strongParaModel.strongClass);
                index = class_MySqlDataBase.ExecuteSql(Sql);
            }
            form_StongParaBase.Dispose();
            return index;
        }
        public override int DeleteData(object id)
        {
            string Sql = string.Format("delete from strongpara where id = '{0}'", id.ToString());
            return class_MySqlDataBase.ExecuteSql(Sql);
        }
        private DataSet GetFinder(int PageNo, int PageSize, string Address)
        {
            string SelectSql = "SELECT *";
            string FormSql = "FROM strongPara";
            string WhereSql = null;
            string GroupSql = null;
            string HavingSql = null;
            string OrderSql = "ORDER BY strongName,sortNo";
            string TotalFieldName = "*";

            PageSqlModel pageSqlModel = class_MySqlDataBase.GetSql(SelectSql
            , FormSql
            , WhereSql
            , GroupSql
            , HavingSql
            , OrderSql
            , PageNo
            , PageSize
            , TotalFieldName);

            return class_MySqlDataBase.GetPageDataSet(pageSqlModel);
        }
        public override void Finder(int PageNo)
        {
            waitDialogForm = new WaitDialogForm("正在玩命加载中......", "温馨提示");

            DataSet dataSet = this.GetFinder(PageNo, base.GetGridPage().PageSize, null);
            base.SetGridDataSource(dataSet.Tables[0]);
            base.GetGridPage().SetRowCount(int.Parse(dataSet.Tables[1].Rows[0][0].ToString()));
            waitDialogForm.Close();
        }

        private void Form_strongPara_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (waitDialogForm != null)
            {
                waitDialogForm.Dispose();
            }
            class_MySqlDataBase.CloseSqlConnection();
        }
    }
}
